<?php
class status{
	private $id;
	private $descricao;
	private $email;

	public function __construct(){
		$this->id = NULL;
		$this->descricao = NULL;
		$this->email = NULL;
	}

	public function get_id(){
		return $this->id;
	}
	public function get_descricao(){
		return $this->descricao;
	}
	public function get_email(){
		return $this->email;
	}
	
	
	public function set_id($id){
		if($id != '' && valida::numero($id)){
                    $this->id = $id;
                    return 1;
		}else{
                    return 0;
                }
        }

	public function set_descricao($descricao){
		$descricao = addslashes($descricao);
		if($descricao != ''){
                    $this->descricao = $descricao;
                    return 1;
		}else{
                    return 0;
                }
        }

	public function set_email($email){
		if(valida::email($email)){
                    $this->email = $email;
                    return 1;
		}else{
                    return 0;
                }
        }

	
	
	public function salvar(){
                $db = new db(config::$driver);
                
                $sql_qnt = "select count(id) from ci_status where id = '".$this->id."'";
                $con = $db->conecta();
                $cnt = $db->query($sql_qnt, $con);
                $id = $db->fetch_array($cnt);
                
                if($id[0] > 0){
                    $sql = "update ci_status set id = '".$this->id."', descricao = '".$this->descricao."', email = '".$this->email."' where id = '$this->id'";
                    $res = $db->query($sql, $con);
                    $id = $this->id;
                }else{
                    $sql = "insert into ci_status(descricao, email) values('".$this->descricao."', '".$this->email."')";
                    
                    $res = $db->query($sql, $con);
                    
                    $sql_id = "select max(id) from ci_status";
                    $res_id = $db->query($sql_id, $con);
                    $id_res = $db->fetch_array($res_id);
                    $id = $id_res;
                }
		
		$db->close($con);

		return $id;
	}
        
	public function carregar($id){
		$sql = "select * from ci_status where id = '$id'";

		$db = new db(config::$driver);
		$con = $db->conecta();
		$res = $db->query($sql, $con);
		$db->close($con);
		$status = $db->fetch_array($res);

		$this->id = $status["id"];
		$this->descricao = $status["descricao"];
		$this->email = $status["email"];
		
		return 1;
	}
}
?>
